<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>峰与谷</title>
</head>
<body>
  <script>
    function fn(nums) {
      // 升序排序
      nums.sort((a, b) => a - b);
      // 头尾依次插入
      let res = [], flag = true;
      while (nums.length) {
        res.push(flag ? nums.pop() : nums.shift());
        flag = !flag;
      }
      // 恢复原数组
      let len = res.length;
      for (let i = 0; i < len; i++) {
        nums[i] = res[i];
      }
      return nums
    }
    console.log(fn([5, 3, 1, 2, 3]))
  </script>
</body>
</html>